package com.jason.fingerprint.store;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class XmlParase {
	
	public static String getNodeString(Element em,String name){
		try{ 
			return em.getElementsByTagName(name).item(0).getFirstChild().getNodeValue();
		}catch(Exception e){ 
			
		}
		return "";
	}
	
	public static ArrayList<Person> parasePersonList(Document doc){
		ArrayList<Person> list = new ArrayList<Person>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Element root = doc.getDocumentElement();
			NodeList items = root.getElementsByTagName("Person");
			if(items.getLength()>0){
				for(int i=0;i<items.getLength();i++){
					Person ri=new Person();
					Element lin = (Element)items.item(i);
					ri.id=getNodeString(lin,"id");
					ri.name=getNodeString(lin,"name");
					
					ri.type=Integer.valueOf(getNodeString(lin,"type"));
					ri.ident=Integer.valueOf(getNodeString(lin,"ident"));
					ri.gender=Integer.valueOf(getNodeString(lin,"gender"));
					ri.phone=getNodeString(lin,"phone");
					
					ri.template1=getNodeString(lin,"template1");
					ri.template2=getNodeString(lin,"template2");
					ri.photo=getNodeString(lin,"photo");
					ri.cardsn=getNodeString(lin,"cardsn");
					ri.barcode1d=getNodeString(lin,"barcode1d");
					ri.barcode2d=getNodeString(lin,"barcode2d");
					
					list.add(ri);
				}
			}
			//inStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static ArrayList<Person> parasePersonList(InputStream inputStream){
		ArrayList<Person> list = new ArrayList<Person>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc=null;
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(inputStream);
			Element root = doc.getDocumentElement();
			NodeList items = root.getElementsByTagName("Person");
			if(items.getLength()>0){
				for(int i=0;i<items.getLength();i++){
					Person ri=new Person();
					Element lin = (Element)items.item(i);
					ri.id=getNodeString(lin,"id");
					ri.name=getNodeString(lin,"name");
							
					list.add(ri);
				}
			}
			//inStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static Document PersonToXml(List<Person> list){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=null;
		try{
		   builder = dbf.newDocumentBuilder();
		}
		catch(Exception e){}
		
		Document doc = builder.newDocument();
		Element root = doc.createElement("Person");
		doc.appendChild(root); //����Ԫ����ӵ��ĵ���
		
		for(int i=0;i<list.size();i++){
			Element le=doc.createElement("Person");
			root.appendChild(le);
			
			Element id=doc.createElement("id");
			le.appendChild(id);
			id.appendChild(doc.createTextNode(list.get(i).id));
			
			Element name=doc.createElement("name");
			le.appendChild(name);
			name.appendChild(doc.createTextNode(list.get(i).name));
			//
			Element type=doc.createElement("type");
			le.appendChild(type);
			type.appendChild(doc.createTextNode(String.valueOf(list.get(i).type)));
			
			Element ident=doc.createElement("ident");
			le.appendChild(ident);
			ident.appendChild(doc.createTextNode(String.valueOf(list.get(i).ident)));
			
			Element gender=doc.createElement("gender");
			le.appendChild(gender);
			gender.appendChild(doc.createTextNode(String.valueOf(list.get(i).gender)));
			
			Element phone=doc.createElement("phone");
			le.appendChild(phone);
			phone.appendChild(doc.createTextNode(list.get(i).phone));
						
			Element template1=doc.createElement("template1");
			le.appendChild(template1);
			template1.appendChild(doc.createTextNode(list.get(i).template1));
			
			Element template2=doc.createElement("template2");
			le.appendChild(template2);
			template2.appendChild(doc.createTextNode(list.get(i).template2));
			
			Element photo=doc.createElement("photo");
			le.appendChild(photo);
			photo.appendChild(doc.createTextNode(list.get(i).photo));
			
			Element cardsn=doc.createElement("cardsn");
			le.appendChild(cardsn);
			cardsn.appendChild(doc.createTextNode(list.get(i).cardsn));
			
			Element barcode1d=doc.createElement("barcode1d");
			le.appendChild(barcode1d);
			barcode1d.appendChild(doc.createTextNode(list.get(i).barcode1d));
			
			Element barcode2d=doc.createElement("barcode2d");
			le.appendChild(barcode2d);
			barcode2d.appendChild(doc.createTextNode(list.get(i).barcode2d));
			
		}
		
		return doc;
	}
	
	//XML File Write
		public static void WriteXmlFile(Document doc,String filename,String codetype) {
			   try {
				   FileOutputStream fos = new FileOutputStream(filename);
				   OutputStreamWriter outwriter = new OutputStreamWriter(fos);
				   try {
						Source source = new DOMSource(doc);
						Result result = new StreamResult(outwriter);
						Transformer xformer = TransformerFactory.newInstance().newTransformer();
						xformer.setOutputProperty(OutputKeys.ENCODING, codetype);	//"gb2312" "utf-8"
						xformer.transform(source, result);
					}
					catch (TransformerConfigurationException e) {
						e.printStackTrace();
					}
					catch (TransformerException e) {
						e.printStackTrace();
					}
				   outwriter.close();
				   fos.close();
			   } catch (Exception e) {
				   e.printStackTrace();
			   }
		}
		
		//XML File Read
		public static Document ReadXmlFile(String filename){
			FileInputStream fin=null;
			InputStream inStream=null;
			Document doc=null;
			try {
				fin = new FileInputStream(filename);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}		
			inStream = new BufferedInputStream(fin);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			try {
				DocumentBuilder builder = factory.newDocumentBuilder();
				//���ַ�����
				//ByteArrayInputStream is = new ByteArrayInputStream(txt.getBytes());
				doc = builder.parse(inStream);			
				//inStream.close();			
			} catch (Exception e) {
				e.printStackTrace();
			}
			return doc;
		}
		
}
